import {Line, BufferGeometry, Float32BufferAttribute, LineBasicMaterial, Mesh, MeshBasicMaterial} from "../../../modules/three/three.module.js";

class PlaneHelper2 extends Line {

  constructor( plane, size = 1, hex = 0xffff00, pos ) {

    const color = hex;

    const positions = [ 1,-1,0, -1,1,0, -1,-1,0, 1,1,0, -1,1,0, -1,-1,0, 1,-1,0, 1,1,0 ];

    const geometry = new BufferGeometry();
    geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) );
    geometry.computeBoundingSphere();

    super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) );

    this.type = 'PlaneHelper';

    this.plane = plane;

    this.size = size;

    const positions2 = [ 1,1,0, - 1,1,0, -1,-1,0, 1,1,0, -1,-1,0, 1,-1,0 ];

    const geometry2 = new BufferGeometry();
    geometry2.setAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) );
    geometry2.computeBoundingSphere();

    this.add( new Mesh( geometry2, new MeshBasicMaterial( { color: color, opacity: 0.2, transparent: true, depthWrite: false, toneMapped: false } ) ) );

    this.scale.set( 0.5 * this.size, 0.5 * this.size, 1 );
    this.lookAt( this.plane.normal );
    // 注意：先调用lookAt设置姿态，再修改位置
    this.position.copy(pos);
  }

  dispose() {

    this.geometry.dispose();
    this.material.dispose();
    this.children[ 0 ].geometry.dispose();
    this.children[ 0 ].material.dispose();

  }

}

export { PlaneHelper2 };
